/* ozogfra 1b
 This file contains the main function
 along with readKeyboard
 for getting the code length and range
*/

#include <iostream>
#include <vector>
#include <stdlib.h>
#include "MasterMind.h"

using namespace std;

bool isValidLength (int digit)
// Returns true if 0 <= digit <= 10
{
  return (digit >= 1 && digit <= 10);
}

bool isValidRange (int digit)
// Returns true if 2 <= digit <10
{
  return (digit <= 10 && digit >= 2);
}

void readKeyboard (int &codeLength, int &codeRange)
// Grabs the codeLength and codeRange from STDIN.
// getline is required to remove newline from 
// istream, since we will be using getline in the
// MasterMind class
{

  printf ("Please enter the length of the secret code: ");
  cin >> codeLength;
  
  if (! isValidLength(codeLength))
    throw baseException("Bad length!");

  printf ("Please enter the number of possible digits in the secret code: ");
  cin >> codeRange;

  if (! isValidRange(codeRange))
    throw baseException("Bad range!");

} //end readKeyboard

int main ()
{
  //Get the length and range from user and check them
  int codeLength, codeRange;
  try
  {
    readKeyboard (codeLength, codeRange);
  }
  catch (baseException error) 
  {
    cerr << error.what() << endl;
    return 1;
  }

  //Mastermind plays the game, which gets guesses from the user.
  //Catch any exceptions due to bad guesses
  MasterMind mm(codeLength, codeRange);
  try
  {
    mm.playGame();
  }
  catch (baseException error) {
    cerr << error.what() << endl;
    return 2;
  }

  return 0;
}  //end main
